<?php

namespace App\Admin\Support;

use App\Models\PreTime as PreTimeModel;
use Dcat\Admin\Grid;
use Dcat\Admin\Support\LazyRenderable;
use Dcat\Admin\Widgets\Table;

class PreTime extends LazyRenderable
{
    public function render()
    {
//        // 获取ID
//        $relId = $this->key;
//
//        // 获取其他自定义参数
//        $type = $this->type;
//        $relType = $this->rel_type;
//
//        $data = PreTimeModel::query()->where(['type' => $type, 'rel_type' => $relType, 'rel_id' => $relId])->get(['id', 'time', 'stock', 'status', 'created_at', 'updated_at'])->toArray();
//
//        $titles = ['时间ID', '时间', '库存', '状态', '创建时间', '更新时间'];
//
//        return Table::make($titles, $data);

        // 获取ID
        $relId = $this->key;

        // 获取其他自定义参数
        $type = $this->type;
        $relType = $this->rel_type;

        return Grid::make(new PreTimeModel(), function (Grid $grid) use ($relId, $type, $relType) {
            $grid->column('id', '时间ID')->sortable();
            $grid->column('time', '时间');
            $grid->column('stock', '库存');
            $grid->column('status', '状态');
            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '更新时间');
            $grid->paginate(10); //每页十条数据
            $grid->disableActions(); //禁止操作
            //设置筛选
            $grid->filter(function (Grid\Filter $filter) use ($relId, $type, $relType) {
                $filter->equal('type')->default($type);
                $filter->equal('rel_type')->default($relType);
                $filter->equal('rel_id')->default($relId);
            });
            $grid->quickSearch('time');
        });
    }

    public function grid(): Grid
    {
        // 获取ID
        $relId = $this->key;

        // 获取其他自定义参数
        $type = $this->type;
        $relType = $this->rel_type;

        return Grid::make(new PreTimeModel(), function (Grid $grid) use ($relId, $type, $relType) {
            $grid->column('id', '时间ID')->sortable();
            $grid->column('time', '时间');
            $grid->column('stock', '库存');
            $grid->column('status', '状态');
            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '更新时间');
            $grid->paginate(10); //每页十条数据
            $grid->disableActions(); //禁止操作
            //设置筛选
            $grid->filter(function (Grid\Filter $filter) use ($relId, $type, $relType) {
                $filter->like('type')->default($type);
                $filter->like('rel_type')->default($relType);
                $filter->like('rel_id')->default($relId);
                $filter->like('time', '时间');
            });
        });
    }
}
